﻿using System;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace com.tiyma.components
{
    public class CrawlerManager
    {
        //空闲内存
        private PerformanceCounter m_memory_counter = new PerformanceCounter("Memory", "Available MBytes");

       

        /// <summary>
        /// 监视程序占用资源
        /// </summary>
        public void WatchCrawler()
        {
            LogHelper.Info("开始侦听收录程序");

            ServiceManager t_service_mgr = new ServiceManager(AppConfig.WithchServiceName);

            DateTime t_date_start = DateTime.Now;

            while (true)
            {
                //1秒
                Thread.Sleep(10000);
                //空闲内存不足500 重启 收录程序
                if (t_service_mgr.ServiceIsStop())
                {
                    t_service_mgr.StartService();

                    t_date_start = DateTime.Now;

                    Thread.Sleep(60 * 60 * 1000);
                }

                //10小时重启一次
                if (DateTime.Now.Subtract(t_date_start).TotalHours >= 10)
                {
                    LogHelper.Info("准备重启收录服务");
                    t_service_mgr.StopService();

                    Thread.Sleep(2 * 60 * 1000);

                    t_service_mgr.StartService();

                    t_date_start = DateTime.Now;

                    LogHelper.Info("重启收录服务成功");
                    Thread.Sleep(60 * 60 * 1000);
                    
                }
            }
        }

        /// <summary>
        /// 每天晚上12点半到7点半之间收录小说章节内容
        /// </summary>
        public void WatchCrawlerChapterContent()
        {
            ServiceManager t_service_mgr = new ServiceManager("tiyma_crawler_content");

            //12点半开始
            DateTime t_start_time = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 0, 30, 0);
            //7点半结束
            DateTime t_end_time = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 7, 30, 0);

            int t_last_sleep_time = 0;

            while (true)
            {

                TimeSpan t_sp_start = t_start_time.Subtract(DateTime.Now);

                t_last_sleep_time = (int)t_sp_start.TotalMilliseconds;

                LogHelper.Info(string.Format("休眠{0}秒之后开始收录章节内容服务", t_sp_start.TotalSeconds));

                Thread.Sleep(t_last_sleep_time);

                //开始收录小说章节内容
                if (t_service_mgr.ServiceIsStop())
                {
                    t_service_mgr.StartService();

                    LogHelper.Info("启动收录章节内容服务");
                   
                }

                TimeSpan t_sp_end = t_end_time.Subtract(t_start_time);

                t_last_sleep_time = (int)t_sp_end.TotalMilliseconds;

                LogHelper.Info(string.Format("休眠{0}秒之后停止收录章节服务", t_sp_end.TotalSeconds));

                Thread.Sleep(t_last_sleep_time);

                //停止收录
                t_service_mgr.StopService();

                LogHelper.Info("停止了收录章节内容服务");

                t_start_time = t_start_time.AddDays(1);
                t_end_time = t_end_time.AddDays(1);

                
            }
        }

        /// <summary>
        /// 启动程序
        /// </summary>
        public void StartCrawlerApp()
        {
            if (!File.Exists(AppConfig.CrawlerAppPath))
            {
                LogHelper.Info("收录小说程序配置路径不存在");
                return;
            }

            try
            {
                Process.Start(AppConfig.CrawlerAppPath);

                LogHelper.Info("成功启动" + AppConfig.CrawlerAppPath);
            }
            catch (Exception ex)
            {
                LogHelper.Error("启动程序失败:" + AppConfig.CrawlerAppPath + ex.ToString());
            }
        }

        private bool IsExistsCrawler()
        {
            Process[] t_all_processes = Process.GetProcesses();

            bool t_is_exists = false;

            foreach (Process p in t_all_processes)
            {         
                if (p.ProcessName.ToLower().Equals("crawler-5"))
                {

                    t_is_exists = true;
                    break;
                }
            }

            return t_is_exists;
        }

        /// <summary>
        /// 停止程序
        /// </summary>
        public void StopCrawlerApp()
        {
            Process[] t_all_processes = Process.GetProcesses();

            foreach (Process p in t_all_processes)
            {
                try
                {
                    if (p.ProcessName.ToLower().Equals("crawler-1"))
                    {
                        
                        p.Kill();
                        LogHelper.Info("傻掉了crawler-1进程");
                    }
                    
                }
                catch (Exception ex)
                {
                    LogHelper.Error("停止应用程序失败" + ex.ToString());
                }
               

            }
        }
    }
}
